//服务器端  管理员添加用户
#include "staff.h"  
/*管理员添加员工*/
int process_admin_adduser_request(int newfd,Msg *msg)
{
	printf("------------%s-----------%d.\n",__func__,__LINE__);
	char sql[DATALEN] = {0};
	char *errmsg;
	char **result;
	int nrow,ncolumn;
	sprintf(sql,"select *from usrinfo where id =%d",msg->info.id);
	if(sqlite3_get_table(db,sql,&result,&nrow,&ncolumn,&errmsg) != SQLITE_OK){
		printf("---****----%s\n",errmsg);
		if(nrow != 0){
			sprintf(msg->recvmsg,"这个工号%d已存在\n",msg->info.id);
			send(newfd,msg,sizeof(Msg),0);
			exit(1);
		}
	}else{
		bzero(sql,sizeof(sql));
		sprintf(sql,"insert into usrinfo values(%d,%d,'%s','%s',%d,'%s','%s','%s','%s',%d,'%lg');"
				,msg->info.id,msg->info.usertype,msg->info.name,msg->info.passwd,msg->info.age,msg->info.telphone
				,msg->info.address,msg->info.position,msg->info.data_entry,msg->info.rate,msg->info.salary);
		if(sqlite3_get_table(db,sql,&result,&nrow,&ncolumn,&errmsg) != SQLITE_OK){
			printf("---****----%s\n",errmsg);
			sprintf(msg->recvmsg,"添加失败%d\n",msg->info.id);
			send(newfd,msg,sizeof(Msg),0);
			exit(1);
		}else{
			time_t t;
			char buf_msg[120] = "";
			char buf_time[512] = "";
			char sql[DATALEN] = "";
			char buf[1024] = "";
			bzero(sql,sizeof(sql));
			bzero(buf,sizeof(buf));
			bzero(buf_msg,sizeof(buf_msg));
			bzero(buf_time,sizeof(buf_time));
			sprintf(buf_msg,"管理员添加员工%s",msg->info.name);
			sprintf(buf_time,"%s",(char*)ctime(&t));
			sprintf(buf,"insert into historyinfo values ('%s','%s');",buf_time,buf_msg);
			sqlite3_get_table(db,buf,&result,&nrow,&ncolumn,&errmsg);
			strcpy(msg->recvmsg,"添加成功");
			send(newfd,msg,sizeof(Msg),0);
		}		
	}
	return 0;
}
